package virtualstock.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import virtualstock.exception.InvalidParameterException;
import virtualstock.exception.PlayerNotFoundException;
import virtualstock.request.GameController;
import virtualstock.request.PlayerController;
import virtualstock.util.GameDetails;
import virtualstock.util.PlayerDetails;

/**
 * Servlet implementation class PlayerLoginServlet
 */
public class PlayerLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	private PlayerController playerController;  
	@EJB
	private GameController gameController;
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PlayerNotFoundException{
    	PrintWriter out = response.getWriter();
    	
    	String email = (String) request.getParameter("email");
    	String password = (String) request.getParameter("password");
    	System.out.println(email);
    	try{
    		List<PlayerDetails> details = playerController.getPlayersByEmail(email);
    		if(details == null){
//    			out.println("no player with this email");
    			response.sendRedirect("error\\errorEmail.html");
    		}
    		else{
    			Iterator<PlayerDetails> iter = details.iterator();
    			PlayerDetails pd = iter.next();
    			if(password.equals(pd.getPassword())){
    				out.println("successfully login");
    				//get all the game this player participates in
    				List<GameDetails> gameList = gameController.getGamesOfPlayer(pd.getPlayerID());
    				System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!");
    				System.out.println(pd.getPlayerID());
    		
    				System.out.println("game list of player " + pd.getPlayerID() + ": " + gameList);
    				GameDetails game = null;
    				for(Iterator<GameDetails> it = gameList.iterator();it.hasNext();){
    					GameDetails gd = it.next();
    					game = gd;
    					System.out.print(gd.getGameID()+" ");
    					System.out.print(gd.getGameName());
    					System.out.println();
    				}
    				//create a session
    				HttpSession session = request.getSession();
    				session.setAttribute("playerDetails", pd);
    				request.setAttribute("playerDetails", pd);
    				request.setAttribute("gameList", gameList);
    				request.setAttribute("game", game);
    				request.getRequestDispatcher("playerHomePage.jsp").forward(request, response);
    			}
    			else{
//    				out.println("error password");
    				response.sendRedirect("error\\errorPassword.html");
    			}
    		}
    		
    	}catch (InvalidParameterException e) {
			out.print("InvalidParameterException");
			e.printStackTrace();
		}
    	
    }
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlayerLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request, response);
		} catch (PlayerNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request, response);
		} catch (PlayerNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
